package sein.android.hopenote;

import java.util.ArrayList;

import sein.android.hopenote.Const.HopeNoteDb;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Normal_Add extends Activity {
	private DBAdapter HnopenHelper;
	private SQLiteDatabase sdb;
	private	Button bt_s, bt_r;
	private	Spinner g_select, i_select;
	private	ArrayList<String> lst_group, lst_item;
	private	ArrayAdapter<String> adapter, adapter_item;
	private	Cursor cur_group, cur_item, cur_save, normal;
	private	TextView tv_fr, tv_to ;
	private EditText et_fr, et_to;
	private String sr_item, sr_user, sr_nf, sr_nt; // Save Button Item
	private ContentValues row, u_values;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        setContentView(R.layout.normal_add);
        
        bt_s = (Button)findViewById(R.id.btn_save);
        bt_r = (Button)findViewById(R.id.btn_resive);
        et_fr = (EditText)findViewById(R.id.fr_nomal_figure);
        et_to = (EditText)findViewById(R.id.to_nomal_figure);
        g_select = (Spinner)findViewById(R.id.group_select);
        i_select = (Spinner)findViewById(R.id.item_select);
        tv_fr = (TextView)findViewById(R.id.frView);
        tv_to = (TextView)findViewById(R.id.toView);
        
        HnopenHelper = new DBAdapter(this);
        sdb = HnopenHelper.getReadableDatabase();
        
        //TB_ITEM Table of COL_GROUP call
        
        cur_group = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_GROUP}, null, null, HopeNoteDb.COL_GROUP, null, null,null);
        cur_group.moveToFirst();
        lst_group = new ArrayList<String>();
        	
        while(!cur_group.isAfterLast()){
			
			String group = cur_group.getString(0);
			lst_group.add(group);
			cur_group.moveToNext();
			//Toast.makeText(this,group ,Toast.LENGTH_SHORT).show();
		}
		
        
        cur_group.close();
        //	The Group Select Spinner Adapter
        g_select.setPrompt("그룹 선택");
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lst_group);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        g_select.setAdapter(adapter);
        
        
        
      //TB_ITEM Table of COL_ITEM call
        String qWhere = HopeNoteDb.COL_GROUP + " = '" + g_select.getSelectedItem().toString() + "'";
        cur_item = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_ITEM}, qWhere, null, null, null, null);
        cur_item.moveToFirst();
        lst_item = new ArrayList<String>();
        while(!cur_item.isAfterLast()){
			
			String item = cur_item.getString(0);
			lst_item.add(item);
			cur_item.moveToNext();
			//Toast.makeText(this,item ,Toast.LENGTH_SHORT).show();
		}
        cur_item.close();
        //The Item Select Spinner Adapter
        
        i_select.setPrompt("항목 선택");
        adapter_item = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lst_item);
        adapter_item.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        i_select.setAdapter(adapter_item);
        
        sr_user = HopeNoteDb.CURRENT_USER;
        String Where = HopeNoteDb.COL_ITEM + " = '" + i_select.getSelectedItem().toString() + "'" + " AND " + HopeNoteDb.COL_USER + "= '" + sr_user +"'";
        normal = sdb.query(HopeNoteDb.TB_NORMAL, new String[] {HopeNoteDb.COL_NORMAL_FR, HopeNoteDb.COL_NORMAL_TO}, Where, null, null, null, null);
        
        if (normal.getCount() > 0) {
		
        	normal.moveToFirst();
			et_fr.setText(normal.getString(0));
			et_to.setText(normal.getString(1));
		}
        
        
        //Reset Item, when Group changed
        g_select.setOnItemSelectedListener(new OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> adapter, View view,
					int id, long arg3) {
				// TODO Auto-generated method stub
				String qWhere = HopeNoteDb.COL_GROUP + " = '" + g_select.getSelectedItem().toString() + "'";
		        cur_item = sdb.query(HopeNoteDb.TB_ITEM, new String[] {HopeNoteDb.COL_ITEM}, qWhere, null, null, null, null);
		        cur_item.moveToFirst();
		        lst_item.clear();
		        while(!cur_item.isAfterLast()){
					String item = cur_item.getString(0);
					lst_item.add(item);
					cur_item.moveToNext();
					//Toast.makeText(this,item ,Toast.LENGTH_SHORT).show();
				}
		        cur_item.close();
		        adapter_item.notifyDataSetChanged();
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
        	
		});
        
        //Normal Range Save Button
        bt_s.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				sr_item = i_select.getSelectedItem().toString();
				sr_user = HopeNoteDb.CURRENT_USER;
				sr_nf = et_fr.getText().toString();
				sr_nt = et_to.getText().toString();
				
				row = new ContentValues();
				row.put(HopeNoteDb.COL_ITEM, sr_item);
				row.put(HopeNoteDb.COL_NORMAL_FR, sr_nf);
				row.put(HopeNoteDb.COL_NORMAL_TO, sr_nt);
				row.put(HopeNoteDb.COL_USER, sr_user);
				
				if(sdb.insert(HopeNoteDb.TB_NORMAL, null, row) > 0)
				{
					Toast.makeText(Normal_Add.this, "저장 완료", Toast.LENGTH_SHORT).show();
					finish();
				}
				else
				{
					Toast.makeText(Normal_Add.this, "실패", Toast.LENGTH_SHORT).show();
				}
			}
        
        });
        bt_r.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				sr_item = i_select.getSelectedItem().toString();
				sr_user = HopeNoteDb.CURRENT_USER;
				sr_nf = et_fr.getText().toString();
				sr_nt = et_to.getText().toString();
				
				u_values = new ContentValues();
				u_values.put(HopeNoteDb.COL_NORMAL_FR, sr_nf);
				u_values.put(HopeNoteDb.COL_NORMAL_TO, sr_nt);				
				
				String qWhere = HopeNoteDb.COL_ITEM + " = '" + sr_item + "'" + " AND " + HopeNoteDb.COL_USER + "= '" + sr_user + "'";
				
				if(sdb.update(HopeNoteDb.TB_NORMAL,u_values, qWhere, null) > 0)
				{
					Toast.makeText(Normal_Add.this, "수정 완료", Toast.LENGTH_SHORT).show();
					finish();
				}
				else
				{
					Toast.makeText(Normal_Add.this, "실패", Toast.LENGTH_SHORT).show();
				}
			}
        
        });
	}
	
	

}
